package com.gmlive.common.ui.xui.view

import android.content.Context
import android.graphics.Canvas
import android.util.AttributeSet
import androidx.annotation.ColorInt
import com.gmlive.common.ui.xui.IXUILayout
import com.gmlive.common.ui.xui.IXUILayout.HideRadiusSide
import com.gmlive.common.ui.xui.alpha.XUIAlphaButton
import com.gmlive.common.ui.xui.helper.XUILayoutHelper

/**
 * 封装的button，便于样式的修改
 *
 * @author xuexiang
 * @since 2019-06-01 20:01
 */
class XUIButton : XUIAlphaButton, IXUILayout {
    private lateinit var mLayoutHelper: XUILayoutHelper

    constructor(context: Context) : super(context) {
        init(context, null, 0)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init(context, attrs, 0)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init(context, attrs, defStyleAttr)
    }

    private fun init(context: Context, attrs: AttributeSet?, defStyleAttr: Int) {
        mLayoutHelper = XUILayoutHelper(context, attrs, defStyleAttr, this)
        setChangeAlphaWhenDisable(false)
        setChangeAlphaWhenPress(false)
    }

    override fun updateTopDivider(topInsetLeft: Int, topInsetRight: Int, topDividerHeight: Int, topDividerColor: Int) {
        mLayoutHelper.updateTopDivider(topInsetLeft, topInsetRight, topDividerHeight, topDividerColor)
        invalidate()
    }

    override fun updateBottomDivider(bottomInsetLeft: Int, bottomInsetRight: Int, bottomDividerHeight: Int, bottomDividerColor: Int) {
        mLayoutHelper.updateBottomDivider(bottomInsetLeft, bottomInsetRight, bottomDividerHeight, bottomDividerColor)
        invalidate()
    }

    override fun updateLeftDivider(leftInsetTop: Int, leftInsetBottom: Int, leftDividerWidth: Int, leftDividerColor: Int) {
        mLayoutHelper.updateLeftDivider(leftInsetTop, leftInsetBottom, leftDividerWidth, leftDividerColor)
        invalidate()
    }

    override fun updateRightDivider(rightInsetTop: Int, rightInsetBottom: Int, rightDividerWidth: Int, rightDividerColor: Int) {
        mLayoutHelper.updateRightDivider(rightInsetTop, rightInsetBottom, rightDividerWidth, rightDividerColor)
        invalidate()
    }

    override fun onlyShowTopDivider(topInsetLeft: Int, topInsetRight: Int,
                                    topDividerHeight: Int, topDividerColor: Int) {
        mLayoutHelper.onlyShowTopDivider(topInsetLeft, topInsetRight, topDividerHeight, topDividerColor)
        invalidate()
    }

    override fun onlyShowBottomDivider(bottomInsetLeft: Int, bottomInsetRight: Int,
                                       bottomDividerHeight: Int, bottomDividerColor: Int) {
        mLayoutHelper.onlyShowBottomDivider(bottomInsetLeft, bottomInsetRight, bottomDividerHeight, bottomDividerColor)
        invalidate()
    }

    override fun onlyShowLeftDivider(leftInsetTop: Int, leftInsetBottom: Int, leftDividerWidth: Int, leftDividerColor: Int) {
        mLayoutHelper.onlyShowLeftDivider(leftInsetTop, leftInsetBottom, leftDividerWidth, leftDividerColor)
        invalidate()
    }

    override fun onlyShowRightDivider(rightInsetTop: Int, rightInsetBottom: Int, rightDividerWidth: Int, rightDividerColor: Int) {
        mLayoutHelper.onlyShowRightDivider(rightInsetTop, rightInsetBottom, rightDividerWidth, rightDividerColor)
        invalidate()
    }

    override fun setTopDividerAlpha(dividerAlpha: Int) {
        mLayoutHelper.setTopDividerAlpha(dividerAlpha)
        invalidate()
    }

    override fun setBottomDividerAlpha(dividerAlpha: Int) {
        mLayoutHelper.setBottomDividerAlpha(dividerAlpha)
        invalidate()
    }

    override fun setLeftDividerAlpha(dividerAlpha: Int) {
        mLayoutHelper.setLeftDividerAlpha(dividerAlpha)
        invalidate()
    }

    override fun setRightDividerAlpha(dividerAlpha: Int) {
        mLayoutHelper.setRightDividerAlpha(dividerAlpha)
        invalidate()
    }

    override var hideRadiusSide: Int
        get() = mLayoutHelper.hideRadiusSide
        set(hideRadiusSide) {
            mLayoutHelper.hideRadiusSide = hideRadiusSide
            invalidate()
        }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        var widthMeasureSpec = widthMeasureSpec
        var heightMeasureSpec = heightMeasureSpec
        widthMeasureSpec = mLayoutHelper.getMeasuredWidthSpec(widthMeasureSpec)
        heightMeasureSpec = mLayoutHelper.getMeasuredHeightSpec(heightMeasureSpec)
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val minW = mLayoutHelper.handleMiniWidth(widthMeasureSpec, measuredWidth)
        val minH = mLayoutHelper.handleMiniHeight(heightMeasureSpec, measuredHeight)
        if (widthMeasureSpec != minW || heightMeasureSpec != minH) {
            super.onMeasure(minW, minH)
        }
    }

    override fun setRadiusAndShadow(radius: Int, shadowElevation: Int, shadowAlpha: Float) {
        mLayoutHelper.setRadiusAndShadow(radius, shadowElevation, shadowAlpha)
    }

    override fun setRadiusAndShadow(radius: Int, @HideRadiusSide hideRadiusSide: Int, shadowElevation: Int, shadowAlpha: Float) {
        mLayoutHelper.setRadiusAndShadow(radius, hideRadiusSide, shadowElevation, shadowAlpha)
    }

    override fun setRadiusAndShadow(radius: Int, hideRadiusSide: Int, shadowElevation: Int, shadowColor: Int, shadowAlpha: Float) {
        mLayoutHelper.setRadiusAndShadow(radius, hideRadiusSide, shadowElevation, shadowColor, shadowAlpha)
    }

    override fun setRadius(radius: Int, @HideRadiusSide hideRadiusSide: Int) {
        mLayoutHelper.setRadius(radius, hideRadiusSide)
    }

    override var radius: Int
        get() = mLayoutHelper.radius
        set(radius) {
            mLayoutHelper.radius = radius
        }

    override fun setOutlineInset(left: Int, top: Int, right: Int, bottom: Int) {
        mLayoutHelper.setOutlineInset(left, top, right, bottom)
    }

    override fun setBorderColor(@ColorInt borderColor: Int) {
        mLayoutHelper.setBorderColor(borderColor)
        invalidate()
    }

    override fun setBorderWidth(borderWidth: Int) {
        mLayoutHelper.setBorderWidth(borderWidth)
        invalidate()
    }

    override fun setShowBorderOnlyBeforeL(showBorderOnlyBeforeL: Boolean) {
        mLayoutHelper.setShowBorderOnlyBeforeL(showBorderOnlyBeforeL)
        invalidate()
    }

    override fun setWidthLimit(widthLimit: Int): Boolean {
        if (mLayoutHelper.setWidthLimit(widthLimit)) {
            requestLayout()
            invalidate()
        }
        return true
    }

    override fun setHeightLimit(heightLimit: Int): Boolean {
        if (mLayoutHelper.setHeightLimit(heightLimit)) {
            requestLayout()
            invalidate()
        }
        return true
    }

    override fun setUseThemeGeneralShadowElevation() {
        mLayoutHelper.setUseThemeGeneralShadowElevation()
    }

    override fun setOutlineExcludePadding(outlineExcludePadding: Boolean) {
        mLayoutHelper.setOutlineExcludePadding(outlineExcludePadding)
    }

    override var shadowElevation: Int
        get() = mLayoutHelper.shadowElevation
        set(elevation) {
            mLayoutHelper.shadowElevation = elevation
        }

    override var shadowAlpha: Float
        get() = mLayoutHelper.shadowAlpha
        set(shadowAlpha) {
            mLayoutHelper.shadowAlpha = shadowAlpha
        }

    override var _shadowColor: Int
        get() = mLayoutHelper._shadowColor
        set(shadowColor) {
            mLayoutHelper._shadowColor = shadowColor
        }

    override fun setOuterNormalColor(color: Int) {
        mLayoutHelper.setOuterNormalColor(color)
    }

    override fun dispatchDraw(canvas: Canvas) {
        super.dispatchDraw(canvas)
        mLayoutHelper.drawDividers(canvas, width, height)
        mLayoutHelper.dispatchRoundBorderDraw(canvas)
    }
}